import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mobile_app.core/collection_navigator/application/bloc/bloc.dart';
import 'package:mobile_app.core/collection_navigator/application/models/all_lexical_item_page_model.dart';
import 'package:mobile_app.core/collection_navigator/application/models/collection_selection_page_model.dart';
import 'package:mobile_app.core/collection_navigator/application/models/collection_single_lexical_item_page_model.dart';
import 'package:mobile_app.core/collection_navigator/application/models/single_collection_page_model.dart';
import 'package:mobile_app.core/collection_navigator/application/models/single_custom_collection_folder_page_model.dart';
import 'package:mobile_app.core/collection_navigator/application/models/single_lexical_item_page_model.dart';
import 'package:mobile_app.core/core/application/models/app_navigator_page_model.dart';
import 'package:mobile_app.core/core/application/models/app_navigator_transition_info.dart';
import 'package:mobile_app.core/core/presentation/navigator_keys.dart';
import 'package:mobile_app.core/core/presentation/widgets/app_navigator_observer.dart';
import 'package:mobile_app.core/core/presentation/widgets/app_navigator_page.dart';
import 'package:mobile_app.core/core/presentation/widgets/app_navigator_transition_delegate.dart';
import 'package:mobile_app.core/injection_container.dart' as core_di;
import 'package:mobile_app.home.collections/all_lexical_item_page/presentation/page/page.dart';
import 'package:mobile_app.home.collections/collection_selection_page/presentation/page/page.dart';
import 'package:mobile_app.home.collections/collection_single_lexical_item_page/presentation/page/page.dart';
import 'package:mobile_app.home.collections/single_collection_page/presentation/page/page.dart';
import 'package:mobile_app.home.collections/single_custom_collection_folder_page/presentation/page/page.dart';
import 'package:mobile_app.home.collections/single_lexical_item_page/presentation/page/page.dart';

class CollectionTab extends StatelessWidget {
  const CollectionTab({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final rootBackButtonDispatcher = Router.of(context).backButtonDispatcher!;
    final backButtonDispatcher = ChildBackButtonDispatcher(rootBackButtonDispatcher);
    backButtonDispatcher.takePriority();

    return Router(
      routerDelegate: _CollectionsRouterDelegate(),
      backButtonDispatcher: backButtonDispatcher,
    );
  }
}

class _CollectionsRouterDelegate extends RouterDelegate
    with ChangeNotifier, PopNavigatorRouterDelegateMixin {
  _CollectionsRouterDelegate() : navigatorKey = collectionNavigatorKey;

  @override
  final GlobalKey<NavigatorState> navigatorKey;

  CollectionNavigatorBloc get _bloc {
    return core_di.sl<CollectionNavigatorBloc>();
  }

  AppNavigatorPage _createNavigatorPage({
    required AppNavigatorPageModel pageModel,
  }) {
    late Widget page;

    switch (pageModel) {
      case CollectionSelectionPageModel():
        page = CollectionSelectionPage(
          key: ValueKey(pageModel.id),
          pageModel: pageModel,
        );

      case SingleCollectionPageModel():
        page = SingleCollectionPage(
          key: ValueKey(pageModel.id),
          pageModel: pageModel,
        );

      case CollectionSingleLexicalItemPageModel():
        page = CollectionSingleLexicalItemPage(
          key: ValueKey(pageModel.id),
          pageModel: pageModel,
        );

      case AllLexicalItemPageModel():
        page = AllLexicalItemPage(
          key: ValueKey(pageModel.id),
          pageModel: pageModel,
        );

      case SingleLexicalItemPageModel():
        page = SingleLexicalItemPage(
          key: ValueKey(pageModel.id),
          pageModel: pageModel,
        );

      case SingleCustomCollectionFolderPageModel():
        page = SingleCustomCollectionFolderPage(
          key: ValueKey(pageModel.id),
          pageModel: pageModel,
        );
    }

    return AppNavigatorPage(
      pageModel: pageModel,
      child: page,
    );
  }

  List<AppNavigatorPage> _createNavigatorPages({
    required CollectionNavigatorBlocState state,
  }) {
    final unremovedPageModels = state.pageModels.where((pageModel) {
      return state.pageModelToTransitionInfo[pageModel] is! RemovalNavigatorTransitionInfo;
    });

    final pages = unremovedPageModels.map((pageModel) {
      return _createNavigatorPage(
        pageModel: pageModel,
      );
    }).toList();

    return pages;
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CollectionNavigatorBloc, CollectionNavigatorBlocState>(
      bloc: _bloc,
      builder: (context, state) {
        final pages = _createNavigatorPages(
          state: state,
        );

        return Navigator(
          key: navigatorKey,
          pages: pages,
          onPopPage: (route, result) {
            _bloc.handlePop();
            return false;
          },
          observers: [
            AppNavigatorObserver(
              onPageAddedToNavigator: (pageModel) {
                _bloc.handlePageAdditionToNavigator(
                  pageModel: pageModel,
                );
              },
              onPageRemovedFromNavigator: (pageModel) {
                _bloc.handlePageRemovalFromNavigator(
                  pageModel: pageModel,
                );
              },
            ),
          ],
          transitionDelegate: AppNavigatorTransitionDelegate(
            pageModels: state.pageModels,
            pageModelToTransitionInfo: state.pageModelToTransitionInfo,
          ),
        );
      },
    );
  }

  @override
  Future<void> setNewRoutePath(dynamic configuration) async {}
}
